// 选择城市
var cityAll={};

$("#selectCity").focus(function(){
    $(".selectCity .city").show();
    // var cityNameOrPinYin=$("#selectCity").val();
 
    hot_city(cityAll);
    $(".classify li").on("mouseover",function(){
        $(this).addClass("checkedCity");
        $(this).siblings().removeClass("checkedCity");
    })
    $(".choseCity li").on("mouseover",function(){
        $(this).addClass("checkedCity");
        $(this).siblings().removeClass("checkedCity");
    })

})

$(".city .choseCity").on('click','li',function() {
    // console.log(1);
    var Uarry = $(".choseCity li"); //获取所有的li元素
    var count = $(this).index(); //获取li的下标 
    var Tresult = Uarry.eq(count).text();
    $("#selectCity").val(Tresult);
});

$("#selectCity").blur(function(){    
    // $(".selectCity .city").css("display","none");
    $(".selectCity .city").fadeOut(100);
})

// 封装ajax 返回所有城市
function ajaxRequest(){
    var cityNameOrPinYin=$("#selectCity").val();
    $.ajax({
        // url: "php/city.js",
        url:url+"v1/city/list",
        method: "GET", // get请求
        dataType: "json", // 希望服务器返回json格式数据，并使ajax进行json解析
        data: {
          cityNameOrPinYin: cityNameOrPinYin,
        }, // 按接口文档要求传参
        xhrFields: {
            withCredentials: true // 允许跨域名储存和访问cookie
        }
    })
    .done(function (data) {
        // alert(data.message);
        // console.log(data.data);//所有城市信息
        if(data.code=="success"){
            cityAll=data.data;//保存所有城市信息 
            // cityList = data.data.cityList;
            // cityhot=data.data.hot;

            hotHotal(cityAll);//热门酒店
        }
    })
}
function ajaxCity(cityName){
    $.ajax({
        url:"https://dev.apis.sh/PBMKFkON0/v1/hotel/list",
        method:"GET",
        dataType:"json",
        data:{
            cityName:cityName,
            limit:8,
            page:1
        },
        xhrFields: {
            withCredentials: true // 允许跨域名储存和访问cookie
        }
    })
    .done(function(data){
        console.log(data);
        var str2='<ul class="clearfix">';
        if(data.code=="success"){
            $(".homepage-content").find("ul").html("");
            $(".notFound").hide();
            var abc=data.data.hotelList;
            for(j=0;j<abc.length;j++){
                // console.log(j)
                // console.log(abc[j]);
                var hotal_type=""
                if(abc[j].star_level>3){
                    hotal_type="豪华型";
                }else{
                    hotal_type="高档型";
                }                    
                str2+='<li class="hot-hotal">';
                    str2+='<a href="hoteldetail.html?hotelId='+abc[j]._id+'">';
                    str2+='<img src="'+url_file+abc[j].picture[2]+'">';
                    // str2+='<img src="img/2.jpg">';
                    str2+='<div class="hotal-msg"><div class="clearfix">';
                    str2+='<span class="hotalName" title="'+abc[j].name+'">'+abc[j].name+'</span>';
                    str2+='<span class="hotalType">'+hotal_type+'</span></div>';
                    str2+='<b class="hotalPrice">'+"￥"+abc[j].price+'</b>'+'</div></a></li>';
            }
        }else if(data.code=="hotel_not_found"){            
            $(".homepage-content").find("ul").remove();
            $(".notFound").show();
        }
        str2 += '</ul>';
        $(".homepage-content").append(str2);
    })
}

// 动态添加城市数据
    //动态添加城市类别
var cityTitle=[
    {name:"热门",id:"hot"},
    {name:"ABCDEF",id:"abcdef"},
    {name:"GHJK",id:"ghjk"},
    {name:"LMNPQ",id:"lmnpq"},
    {name:"RSTW",id:"rstw"},
    {name:"XYZ",id:"xyz"}    
];
function addTitle(){
    let str="";
    for(i=0;i<cityTitle.length;i++){
        str+="<li data-id="+cityTitle[i].id+">"+cityTitle[i].name+"</li>";
    }
    $(".classify ul").append(str);
    $(".classify li")[0].className="checkedCity";
}
// 动态添加热门城市
function hot_city(data){
    $(".choseCity").find("ul").remove();
    var str='<ul class="clearfix" data-id="hot">';
    for(i=0;i<data.hot.length;i++){
        str+='<li class="data-item" data-id="hot">'+data.hot[i].cityName+'</li>';
        // console.log(data.hot[i].cityName);
    }
    $(".choseCity").append(str);
}
// 动态添加城市列表
function list_city(){
    $(".classify ul").on("mouseover","li",function(){
        $(this).parent().children().removeClass("checkedCity");
        $(this).addClass("checkedCity");
        var eachCity=[];
        var cityStr='<ul class="clearfix" data-id="'+ $(this).attr("data-id") +'">';
        if( $(this).attr("data-id")=="hot" ){
            for(i=0;i<cityAll.hot.length;i++){
                cityStr+='<li data-id="hot">'+cityAll.hot[i].cityName+'</li>';
            }
        }else{
            for(let i in cityAll.cityList){
                if($(this).attr("data-id").indexOf(cityAll.cityList[i][0].cityFirstLetter) >=0 ){
                    eachCity.push(cityAll.cityList[i]);
                }
            }
            for(i=0;i<eachCity.length;i++){
                for(j=0;j<eachCity[i].length;j++){
                    cityStr+='<li>'+eachCity[i][j].cityName+'</li>';
                }
            }
        }
        cityStr+='</ul>';
        $(".choseCity").find("ul").remove();
        $(".choseCity").append(cityStr);
        
        $(".choseCity li").on("mouseover",function(){
            $(this).addClass("checkedCity");
            $(this).siblings().removeClass("checkedCity");
        })
    })
    
}
$(document).ready(function(){
    ajaxRequest();
    list_city();//城市列表    
    addTitle();
    
})

// 日历

		
var startTime,
    endTime,
    duringDay, startDate, endDate,timeStamp;
laydate.render({
    elem: '#enter_time', //指定元素
    format: 'yyyy年M月dd日',
    range: true,
    min: 0, //今天起
    max: 60, //30天后
    done: function(value, date ,endDate) {
        startDate = value;
        str = date.year + '/' + date.month + '/' + date.date;
        var datex = new Date(str);
        startTime = parseInt(datex.getTime());
        str2 = endDate.year + '/' + endDate.month + '/' + endDate.date;
        var datey =new Date(str2);
        endTime = parseInt(datey.getTime());
        var duringTime = endTime - startTime;
        console.log(duringTime);
        timeStamp= startTime+","+endTime;
    }
});



// 搜索查询
$(".searchBtn").click(function(){
    var cityName=$("#selectCity").val();
    $.ajax({
        url:url+"v1/hotel/list",
        dataType:"json",
        method:"GET",
        data:{
            cityName:cityName,
            limit:3,
            page:1,
            checkTime:timeStamp,
        },
        xhrFields: {
            withCredentials: true // 允许跨域名储存和访问cookie
        }
    }).done(function(data){
        console.log(data);
        if(data.code=="success"){
            var count=data.data.count;
            window.location.href='hotelScreen.html?cityName='+cityName+'?count='+count+''
        }else{
            alert(data.message);
        }
    })
})


// 页面加载时显示的热门酒店
function hotHotal(data){
    $(".homepage-title").find("ul").remove();
    var str='<ul class="hotul clearfix" data-id="hotCity">';
    for(i=0;i<data.hot.length;i++){
        str+='<li class="hot-city" data-id="'+data.hot[i].cityName+'">'+data.hot[i].cityName+'<i></i>'+'</li>';
        if(data.hot[i].cityName=="北京"){
            ajaxCity(data.hot[i].cityName);
        }
    }
    $(".homepage-title").append(str);
    $(".hotul li").click(function(){  //热门酒店选中城市事件
        $(".hotul li i").css("display","none");
        console.log("hot-city is be clicked");
        $(this).find("i").css("display","block");
        var cityName=$(this).attr("data-id");
        // console.log(cityName);        
        ajaxCity(cityName);
    })
}

